package com.undsf.crawler

import org.ini4j.Config
import org.ini4j.Ini
import redis.clients.jedis.Jedis
import sun.security.util.Resources

/**
 * Created by Arathi on 2017/9/13.
 */
abstract class CachedCrawler extends BaseCrawler {
    // String redis
    String hashKey
    Jedis redis

    CachedCrawler() {
        initCrawler()
        initRedis()
    }

    def initCrawler() {
        CrawlerConfig conf = CrawlerConfig.GetInstance()
        hashKey = conf.getString("Crawler", "hash_key", "pages")
    }

    def initRedis() {
        CrawlerConfig conf = CrawlerConfig.GetInstance()
        String host = conf.getString("Redis", "host", "127.0.0.1")
        int port = conf.getInteger("Redis", "port", 6379)
        redis = new Jedis(host, port)
    }

    def getFromCache(String url) {
        boolean exists = redis.hexists(hashKey, url)
        String content
        if (!exists) {
            content = sendGetRequest(url)
            redis.hset(hashKey, url, content)
        }
        else {
            content = redis.hget(hashKey, url)
        }
        return content
    }

    def getFromCache(String url, Map<String,String> params) {
        StringBuffer urlFull = new StringBuffer()
        urlFull.append(url)

        int counter = 0
        for (Map.Entry<String,String> entry : params.entrySet()) {
            if (counter <= 0) {
                urlFull.append("?")
            }
            else {
                urlFull.append("&")
            }
            urlFull.append(entry.getKey() + "=" + entry.getValue())
            counter++
        }

        return getFromCache(urlFull)
    }
}
